export default {
	getCookie (name) {
		var arr = document.cookie.replace(/\s/g, '').split(';')
		for (var i = 0; i < arr.length; i++) {
			var tempArr = arr[i].split('=')
			if (tempArr[0] === name) {
				return decodeURIComponent(tempArr[1])
			}
		}
		return ''
	},
	setCookie (name, value, days) {
		var date = new Date()
		date.setDate(date.getDate() + days)
		document.cookie = name + '=' + value
	},
	delCookie (name) {
		this.setCookie(name, '1', -1)
	},
	hasCookie (name) {
		return (new RegExp('(?:^|;\\s*)' + encodeURIComponent(name).replace(/[-.+*]/g, '\\$&') + '\\s*\\=')).test(document.cookie)
	},
	getAllCookie () {
		var cookieArr = document.cookie.replace(/((?:^|\s*;)[^=]+)(?=;|$)|^\s*|\s*(?:=[^;]*)?(?:\1|$)/g, '').split(/\s*(?:=[^;]*)?;\s*/)
		for (var nIdx = 0; nIdx < cookieArr.length; nIdx++) {
			cookieArr[nIdx] = decodeURIComponent(cookieArr[nIdx])
		}
		return cookieArr
	},
	getQuery (name) {
		// 正则：[找寻'&' + 'url参数名字' = '值' + '&']（'&'可以不存在）
		let reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)')
		let r = window.location.search.substr(1).match(reg)
		if (r != null) {
			// 对参数值进行解码
			return decodeURI(r[2])
		}
		return null
	},
	// 过滤为空的字段
	filterData (data) {
		for (const key in data) {
			if (data[key] === '' || data[key] === undefined) {
				delete data[key]
			}
		}
		return data
	},
	// 数字中文转换
	SectionToChinese (section) {
		var chnNumChar = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九']
		// var chnUnitSection = ['', '万', '亿', '万亿', '亿亿']
		var chnUnitChar = ['', '十', '百', '千']
		var strIns = ''
		var chnStr = ''
		var unitPos = 0
		var zero = true
		while (section > 0) {
			var v = section % 10
			if (v === 0) {
				if (!zero) {
					zero = true
					chnStr = chnNumChar[v] + chnStr
				}
			} else {
				zero = false
				strIns = chnNumChar[v]
				strIns += chnUnitChar[unitPos]
				chnStr = strIns + chnStr
			}
			unitPos++
			section = Math.floor(section / 10)
		}
		return chnStr
	}
}
